<?php 
    require_once("includes/php/loginfuncs.php");
    $allowed_to_edit = 0;
    if ($_SESSION[id]) 
    { 
        $result = mysql_query("SELECT content, doc_name, date_updated from documents WHERE doc_id = $doc_id AND user_id = $_SESSION[id]");
        $row = mysql_fetch_row($result);
    
        if ($row) 
        {  
            $content = json_encode($row[0]); 
            $doc_name = $row[1];
            $date_updated = $row[2]; 
            $allowed_to_edit = TRUE;   
        }
    }
?>

<!DOCTYPE html>
<html>
<head>
<title> simpPL Editor </title> 
<link rel="stylesheet" type="text/css" href="assets/css/editor.css" />
<link rel="stylesheet" type="text/css" href="assets/css/site.css" />
<link rel="stylesheet" type="text/css" href="assets/css/login_panel/slide.css" media="screen" />
<link rel="stylesheet" type="text/css" href="assets/css/ui-darkness/jquery-ui-1.8.14.custom.css" media="screen" />
<script src="assets/js/editor/src/ace.js" type="text/javascript" charset="utf-8"></script>
<script src="assets/js/editor/src/theme-cobalt.js" type="text/javascript" charset="utf-8"></script>
<script src="assets/js/editor/src/mode-javascript.js" type="text/javascript" charset="utf-8"></script>

<script type="text/javascript" src="assets/js/jquery-1.5.1.min.js"></script>   
<script type="text/javascript" src="assets/js/jquery-ui-1.8.14.custom.min.js"></script>   
<script type="text/javascript" src="assets/js/JSON.js"></script>   
<script type="text/javascript" src="assets/js/timeago.js"></script>  
 <script type="text/javascript" src="assets/js/compiler/src/compiler.js"></script> 
<script src="assets/js/login_panel/slide.js" type="text/javascript"></script>
<?php echo $script; ?>
<script>
 $(document).ready(function() {
     jQuery("span.timeago").timeago($("span.timeago").attr("title"));
      $( "#accordion" ).accordion({collapsible:true, autoHeight:false});
      $( "#dialogWishToSave").dialog({ autoOpen: false, resizable:false, modal:true }); 
      $( "button" ).button();
      $( "#homeButton" ).click(function() { 		
            $( "#dialogWishToSave" ).dialog('open'); return false; 
      });
      $( "#runButton" ).click(function () { 
           compiler = new Compiler(editor.getSession().getValue()); 
           window.alert(compiler._getData());    
      }); 
 
      $( "#saveButton" ).click(function () {  
                var content = JSON.encode(editor.getSession().getValue()); 
                $.ajax({ 
                    type:"POST", 
                    url:"includes/php/docajax.php", 
                    data:"action=save&content="+content+"&doc_id=<?php echo $doc_id;?>",
                    success: function(xhr){  
                        if (xhr) 
                        {
                            //TODO add in Saving, Saved thing in top right 
                            $("span.timeago").html(jQuery.timeago(xhr));  
                            $("span.timeago").attr("title", xhr);
                        }
                    }
            }); 
        }); 
    $("#dialogWishToSave").dialog('option', 'buttons', {  
            "Yes" : function() { 
                var content = JSON.encode(editor.getSession().getValue()); 
                $.ajax({
                    type:"POST", 
                    url:"includes/php/docajax.php", 
                    data:"action=save&content="+content+"&doc_id=<?php echo $doc_id;?>",
                    success: function(xhr){ 
                        window.location.href = '/indstudy/index.php';  
                    } 
                }); 
            },
            "No" : function() { 
                window.location.href = '/indstudy/index.php';  
            },
            "Cancel" : function() {
                $(this).dialog("close"); 
            },
     });
 
 }); 
</script>
<script>
var editor; 
window.onload = function() {
    editor = ace.edit("SE_editor");
    editor.setTheme("ace/theme/cobalt");
    editor.getSession().setValue(<?php echo $content ?>); 
    var JavaScriptMode = require("ace/mode/javascript").Mode;
    editor.getSession().setMode(new JavaScriptMode());
    editor.getSession().setUseWrapMode(true);
};

</script>
</head>
<body>
<?php include("./includes/header.html"); ?>
<div id="SE_siteContainerTopper"> 
     <?php
      if ($allowed_to_edit) :  
        echo "<span style='display: inline; float:left'>Currently editing Project : $doc_name</span>"; 
     ?> 
      <div id="savingContainer">
            <span style = "display: inline; float: right" title="<?php echo $date_updated?>" class="timeago"></span>
            <span style = "display: inline; float: right">Last Saved : </span> 
      </div>
</div> 
<div id="SE_siteContainer">
     <div id="SE_optionsWrapper">
         <div id="SE_buttonGroup"> 
             <button class="SE_editorButton" id="homeButton">Home</button><br>
                     <div id="dialogWishToSave" title="Save?">
                         <div>Would you like to save? All unsaved changes will be lost.</div> 
                    </div>
         </div>
         <div id="accordion" style="float:left; display:inline">
             <h3><a href="#">Recently Edited</a></h3> 
             <div id="SE_recentProjects"> 
                 <div class='bold'>Click to open.</div>
                 <?php
                     // this var comes from the include in header.html, prevents multiple sql queries for same 
                     // data   
                     echo $recentDocuments; 
                  ?>
             </div>
             <h3><a href="#">Document Functions</a></h3> 
             <div> 
                 Add functions to be executed <br> 
                 Find, Print, etc. 
             </div>
             <h3><a href="#">Help</a></h3> 
             <div> 
                 FAQs
             </div>
         </div>
         <div id="SE_buttonGroup"> 
             <button class="SE_editorButton" id="saveButton">Save</button><br> 
             <button class="SE_editorButton" id="runButton">Run</button><br>
         </div> 
     </div> 
     <div id="SE_editor" style="float: left; display: inline;">
     </div>
    <?php 
       else : 
    ?> 
        <div id="SE_siteContainer">
           <div id="SE_notAllowed"> 
              <h1>You are not allowed to edit this project!!</h1>
              <a href='/indstudy/index.php'>Home</a>
              <p>If you feel you've reached this page in error. Please Contact Us.</p>
           </div> 
        </div>
    <?php 
       endif;
    ?> 
<?php include("./includes/footer.html"); ?>
</div> 
</body>
</html> 
